package com.kong.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.kong.vo.ExcelVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author: Kong
 * @DateTime: 2022/9/14 21:43
 * @Description: TODO
 */
@RestController
@RequestMapping("/upload")
public class UploadController {

    @RequestMapping("/importExcel")
    public void importExcel(@RequestParam("file")MultipartFile file){
        try {
            EasyExcel.read(file.getInputStream())
                    .head(ExcelVo.class)
                    .sheet()
                    .registerReadListener(new AnalysisEventListener<ExcelVo>() {

                        @Override
                        public void invoke(ExcelVo excelVo, AnalysisContext analysisContext) {
                            System.out.println(excelVo);
                        }

                        @Override
                        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                            System.out.println("=========文件解析完成=========");
                        }
                    }).doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
